﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSC_OTC_Management.Forms.SoTraiChu
{
    public partial class Frm_UpMauSo : Form
    {
        #region Khai bao bien
        Code.CL_Ultility _cl = new Code.CL_Ultility();
        string fileName = "";
        string fullNamePath = "";
        string fileChonFullPath = "";
        string fileChonName = "";
        string idMaTP = "";
        #endregion

        public Frm_UpMauSo()
        {
            InitializeComponent();
        }

        #region Events

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnXemLaiMauCu_Click(object sender, EventArgs e)
        {
            byte[] result =  _cl._services.GetDocument(this.fileName);
            _cl.SaveDocument(result, this.fullNamePath);
            _cl.RunFile(this.fullNamePath);
        }

        private void btnChonMauSo_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.fileChonFullPath = openFileDialog1.FileName;
                this.fileChonName = openFileDialog1.SafeFileName;
                this.lblFileName.Text = openFileDialog1.SafeFileName;
                this.btnUp.Visible = true;
                this.lblFileName.Visible = true;
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] _data = this._cl.GetDocument(this.fileChonFullPath);
                if (this._cl._services.UpdateTenMauSoTP(this.idMaTP,this.fileChonName) == "0")
                {
                    MessageBox.Show("Up mẫu sổ thành công");
                    this._cl._services.SaveDocument(this.fileChonName, _data);
                }
                else
                {
                    MessageBox.Show("Up thất bại, vui lòng liên hệ IT");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtTCPH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                try
                {
                    //chuyển sang Form View để tìm kiếm Sổ trái chủ
                    Code.StaticVariable.menuID_Active = "10";
                    Forms.FrmView _FrmView = new Forms.FrmView("BSC_TBL_TPPH", "Thông tin TPPH", true);
                    _FrmView.ShowDialog();

                    //lấy về dữ liệu DataTable
                    DataTable tableResult = _cl._services.Get_Table_TPPH_BY_ID(Code.StaticVariable.id_TruyVan);
                    if (tableResult.Rows.Count >0)
                    {
                        this.txtTCPH.Text = tableResult.Rows[0]["TENTP"].ToString();
                        this.idMaTP = Convert.ToString(tableResult.Rows[0]["MATP"]); ;
                        this.lblChonMauSo.Visible = true;
                        this.btnChonMauSo.Visible = true;
                        // hien ra các control ẩn hay ko
                        if (tableResult.Rows[0]["MAUSOTC"].ToString() != "")
                        {
                            this.lblTCHP.Visible = true;
                            this.btnXemLaiMauCu.Visible = true;
                            this.fileName = tableResult.Rows[0]["MAUSOTC"].ToString();
                            this.fullNamePath = Code.StaticVariable.v_StartUP_Path + "\\" + this.fileName;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Bạn phải chọn trái phiếu!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
        #endregion

        #region hàm

        #endregion
    }
}
